package org.ilovecode.display.scrollableWindow {
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	import gs.easing.*;
	
	import org.ilovecode.control.Scrollbar;
	import org.ilovecode.mvc.View;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class ScrollableWindow extends View {
		
		//{ region Stage elements
		
		private var _scrollbar:Scrollbar;
		private var _content:Sprite;
		private var _scrollMask:Sprite;
		
		private var _width:Number;
		private var _height:Number;
		
		private const CONTENT_OFFSET:uint = 10;
		
		//} endregion
		
		public function ScrollableWindow(content:Sprite = null, width:Number = 0, height:Number = 0) {
			super();
			
			_content = content;
			_width = width;
			_height = height;
		}
		
		override protected function construct():void
		{
			if (!_content)
				return;
			
			this.addChild(_content);
			
			_scrollbar = new Scrollbar();
			this.addChild(_scrollbar);
			
			_scrollMask = new Sprite();
			_scrollMask.graphics.beginFill(0xFFFFFF);
			_scrollMask.graphics.drawRect(0, 0, _width, _height);
			_scrollMask.graphics.endFill();
			this.addChild(_scrollMask);
			
			_content.mask = _scrollMask;
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			_scrollbar.x = _width - _scrollbar.width;
			//_scrollbar.scrollArea.height = _height;
			_scrollbar.visible = (_content.height > _scrollbar.height);
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			_scrollbar.addEventListener(Event.SCROLL, update);
			_scrollbar.addEventListener(Event.COMPLETE, update);
			_content.addEventListener(Event.RESIZE, update);
			this.addEventListener(MouseEvent.MOUSE_WHEEL, this_MouseWheel);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			_scrollbar.removeEventListener(Event.SCROLL, update);
			_scrollbar.removeEventListener(Event.COMPLETE, update);
			_content.removeEventListener(Event.RESIZE, update);
			this.removeEventListener(MouseEvent.MOUSE_WHEEL, this_MouseWheel);
		}
		
		override protected function update(event:Event):void
		{
			switch(event.type)
			{
				case Event.SCROLL:
					_scrollbar.addEventListener(Event.ENTER_FRAME, scroll);
					break;
				case Event.COMPLETE:
					_scrollbar.removeEventListener(Event.ENTER_FRAME, scroll);
					break;
				case Event.RESIZE:
					var scrollingArea:Number = _content.height + CONTENT_OFFSET - _scrollMask.height;
					_scrollbar.setScrollPositionRatio(- _content.y / scrollingArea);
					//scroll();
					break;
				default:
					super.update(event);
					break;
			}
		}
		
		
		public function clear():void {
			while (this._content.numChildren > 0) {
				this._content.removeChildAt(0);
			}
		}
		
		//{ region Handlers
		
		/*private function scrollbar_Scroll(event:Event):void {
			_scrollbar.addEventListener(Event.ENTER_FRAME, scroll);
		}
		
		private function scrollbar_Complete(event:Event):void {
			_scrollbar.removeEventListener(Event.ENTER_FRAME, scroll);
		}*/
		
		private function this_MouseWheel(event:MouseEvent):void {
			_scrollbar.moveHandle(event.delta);
			scroll();
		}
		
		//} endregion
		
		private function scroll(event:Event = null):void {
			var scrollingArea:Number = _content.height + CONTENT_OFFSET - _scrollMask.height;
			var contentPosition:Number = - _scrollbar.getScrollPositionRatio() * scrollingArea;
			TweenLite.to(_content, 0.2, { y:contentPosition, ease:Linear.easeOut } );
		}
		
	}

}